package com.hmdp.test.lifecycle.ext;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * 在 BeanDefinition 和完整 BeanDefinition 中间通过一个后置增强器，可以对bean的定义信息进行统一修改，只需要实现 BeanFactoryPostProcessor 接口即可，
 * 这个后置增强器是可以有多个的，你只要在不同的类实现多个 BeanFactoryPostProcessor 接口就会执行多次
 *
 * @author lizhihui
 */
@Slf4j
@Component
public class AMyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        // This allows for overriding or adding properties even to eager-initializing beans.
        final BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition("myBean");
        // beanDefinition.getPropertyValues().addPropertyValue("version", 1);

        log.error("0.BeanDefinition 的后置增强器 - MyBeanFactoryPostProcessor.postProcessBeanFactory - beanDefinition={}", JSONUtil.toJsonStr(beanDefinition));
    }

}
